package com.example.springboot.jpa.dao;

import com.example.springboot.jpa.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class UserTemplateDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public User getUserById(String username) {
		List<User> list = jdbcTemplate.query("select * from t_user where user_name='" + username + "'",
				new RowMapper<User>() {
					@Override
					public User mapRow(ResultSet rs, int rowNum) throws SQLException {
						User user = new User();
						user.setId(rs.getInt("id"));
						user.setUserName(rs.getString("user_name"));
						user.setPassword("password");
						return user;
					}
				});
		User user = new User();
		if (list != null && list.size() > 0) {
			user = list.get(0);
		}
		return user;
	}
}
